/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JOptionPane;
import modelo.GestorArticulos;
import persistencia.AlmacenDAO;
import pojos.Almacen;
import pojos.Articulos;


/**
 *
 * @author David Rial
 */
public class DialogEditArticulos extends javax.swing.JDialog {

    /**
     * Creates new form DialogAddArticulos
     */
    Articulos articulo;
    AlmacenDAO almacenDAO;
    List<Almacen> alAlmacenes;
    GestorArticulos gestorArticulos;
    public DialogEditArticulos(java.awt.Frame parent, boolean modal,Articulos a) {
        super(parent, modal);
        initComponents();
        this.setTitle("Modificar Articulo");
        this.articulo = a;
        almacenDAO = new AlmacenDAO();
        gestorArticulos = new GestorArticulos();
        jcbIDAlmacen.addItem(articulo.getAlmacen().getIdAlmacen());
        jtfDescripcion.setText(articulo.getDescripcion());
        jtfPrecio.setText(Integer.toString(articulo.getPrecio()));
        alAlmacenes = almacenDAO.getAlmacenes();
        for (int i = 0; i < alAlmacenes.size(); i++) {
            jcbIDAlmacen.addItem(alAlmacenes.get(i).getIdAlmacen());
        }
        
        //evento que modifica un articulo de la base de datos previamente  en funcion de los campos introducidos
        //en los textfields
        jbEdit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                //se pide confirmacion para estar seguros de que el cliente quiere modificar este articulo
                int dialogButton = JOptionPane.YES_NO_OPTION;
                dialogButton = JOptionPane.showConfirmDialog (null, "¿Estas seguro de querer editar el articulo?","Advertencia",dialogButton);
                if(dialogButton == JOptionPane.YES_OPTION){
                    try{
                        Almacen almacen;
                        almacen = almacenDAO.getAlmacen((int) jcbIDAlmacen.getSelectedItem());
                        articulo.setAlmacen(almacen);
                        articulo.setDescripcion(jtfDescripcion.getText());
                        articulo.setPrecio(Integer.parseInt(jtfPrecio.getText()));
                        gestorArticulos.editArticulo(articulo);
                        JOptionPane.showMessageDialog(jbEdit, "Se ha modificado el articulo.");
                        dispose();//metodo para cerrar solamente el dialog
                    }catch(NumberFormatException e){
                        JOptionPane.showMessageDialog(jbEdit, "Introduce un numero en el campo precio.");
                    }
                }
            }
        });
        //evento que cancela la operacion de modificar sin que ningun articulos se vea afectado por algun cambio
        jbCancela.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dispose();
            }
        });
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlIdalmacen = new javax.swing.JLabel();
        jcbIDAlmacen = new javax.swing.JComboBox();
        jlDescripcion = new javax.swing.JLabel();
        jtfDescripcion = new javax.swing.JTextField();
        jlPrecio = new javax.swing.JLabel();
        jtfPrecio = new javax.swing.JTextField();
        jbEdit = new javax.swing.JButton();
        jbCancela = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jlIdalmacen.setText("ID Almacen:");

        jlDescripcion.setText("Descripcion:");

        jlPrecio.setText("Precio:");

        jbEdit.setText("Modificar");

        jbCancela.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlIdalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlDescripcion)
                            .addComponent(jlPrecio))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jcbIDAlmacen, 0, 128, Short.MAX_VALUE)
                            .addComponent(jtfDescripcion)
                            .addComponent(jtfPrecio)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jbEdit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbCancela)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlIdalmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbIDAlmacen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlDescripcion)
                    .addComponent(jtfDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlPrecio)
                    .addComponent(jtfPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbEdit)
                    .addComponent(jbCancela))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    /**
     * @param args the command line arguments
     */

//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(DialogEditArticulos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(DialogEditArticulos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(DialogEditArticulos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(DialogEditArticulos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//        //</editor-fold>
//
//        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                DialogEditArticulos dialog = new DialogEditArticulos(new javax.swing.JFrame(), true, new Articulos());
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbCancela;
    private javax.swing.JButton jbEdit;
    private javax.swing.JComboBox jcbIDAlmacen;
    private javax.swing.JLabel jlDescripcion;
    private javax.swing.JLabel jlIdalmacen;
    private javax.swing.JLabel jlPrecio;
    private javax.swing.JTextField jtfDescripcion;
    private javax.swing.JTextField jtfPrecio;
    // End of variables declaration//GEN-END:variables
}
